<template>
	<view class="warp">
		<!-- 	<view class="header">
			<uni-nav-bar left-icon="left" @clickLeft="leftClick" :border="false" title="导航栏组件"></uni-nav-bar>
		</view> -->
		<view class="scroll-view">
			<!-- <view class="all-history-loaded">
				{{allHistoryLoaded ? '已经没有更多的历史消息' : '下拉获取历史消息'}}
			</view> -->
			<view class="talk-list">
				<view v-for="(item,index) in talkList" :key="index" :id="`msg-${item.id}`">
					<view class="item flex_col" :class=" item.type == 1 ? 'pull':'push' ">
						<image :src="item.pic" mode="aspectFill" class="pic"></image>
						<view class="content2" v-if="item.images" @click="imgListPreview(item)">
							<image :src="item.images" mode="widthFix"></image>
						</view>
						<view class="content" v-else>
							{{item.content}}
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="action-box">
			<view class="action-top">
				<view :class="[audio.visible ? 'record-icon record-open':'record-icon']" @click="switchAudioKeyboard">
					<image src="../../static/images/news/record-appearance-icon.png" mode="widthFix"></image>
				</view>
				<view class="record-input" @touchstart="onRecordStart" @touchend="onRecordEnd" v-if="audio.visible">
					{{audio.recording ? '松开发送' : '按住录音'}}
				</view>
				<view class="message-input" v-else>
					<!-- GoEasyIM最大支持3k的文本消息，如需发送长文本，需调整输入框maxlength值 -->
					<input type="text" maxlength="700" placeholder="发送消息" v-model="content">
				</view>
				<view class="file-icon emoji-icon" @click="showEmoji">
					<image src="../../static/images/news/emoji.png" mode="widthFix"></image>
				</view>
				<view class="file-icon more-icon" @click="showOtherTypesMessagePanel">
					<image src="../../static/images/news/more.png" mode="widthFix"></image>
				</view>
				<span class="send-message-btn" @click="createTextMessage">发送</span>
			</view>
			<view class="" id="box">
				<uni-transition :show="emojShow">
					<view class="transition">
						<swiper class="slider" :current="emojIndex" indicator-dots="true">
							<swiper-item v-for="(item, key) in emojiData" :key="key" class="slider-emoji"
								:class="[key==emojIndex?'lastbox':'']">
								<text v-for="(emoji, index) in item" :key="index" @click="selemoji(emoji)"
									class="slider-emoji-icon">{{ emoji }}</text>
							</swiper-item>
						</swiper>
					</view>
				</uni-transition>
			</view>
			<view class="action-bottom">
				<uni-transition :show="otherTypesMessagePanelVisible">
					<view class="action-flex">
						<view class="more-item" @click="createImageMessage">
							<view class="more-img">
								<image src="../../static/images/news/xiance.png" mode="widthFix"></image>
							</view>
							<view>图片</view>
						</view>
						<view class="more-item" @click="createVideoMessage">
							<view class="more-img">
								<image src="../../static/images/news/shiping.png" mode="widthFix"></image>
							</view>
							<view>视频</view>
						</view>
						<view class="more-item" @click="showCustomMessageForm">
							<view class="more-img">
								<image src="../../static/images/news/wenjian.png" mode="widthFix"></image>
							</view>
							<view>文件</view>
						</view>
					</view>
				</uni-transition>
			</view>
		</view>
	</view>
</template>

<script>
	import emoji from "@/js_sdk/m-emoji/m-emoji_2.0.0/emoji.js";
	export default {
		data() {
			return {
				audio: {
					startTime: null,
					//语音录音中
					recording: false,
					//录音按钮展示
					visible: false
				},
				content: '',
				talkList: [],
				emojShow: false,
				emojiData: [],
				emojIndex: 0,
				otherTypesMessagePanelVisible: false
			};
		},
		onShow() {
			var page = Math.ceil(emoji.length / 250);
			for (let i = 0; i < page; i++) {
				this.emojiData[i] = [];
				for (let k = 0; k < 21; k++) {
					emoji[i * 21 + k] ? this.emojiData[i].push(
						emoji[i * 21 + k]
					) : ''
				}
			}
		},
		methods: {
			leftClick() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 发送内容
			createTextMessage() {
				if (!this.content) {
					uni.showToast({
						title: '请输入有效的内容',
						icon: 'none'
					})
					return;
				};
				// let peerId = document.getElementById("peerId").value.toString()
				// 对端用户的 uid。
				let PeerId = this.uid;
				// 发送点对点消息
				let peerMessage = {
					text: this.content
				};
				let data = {
					"id": new Date().getTime(),
					"content": this.content,
					"type": 2,
					"pic": "https://scpic1.chinaz.net/Files/pic/pic9/202205/apic41054_s.jpg"
				}
				this.talkList.push(data);
				this.$nextTick(() => {
					// 清空内容框中的内容
					this.content = '';
					uni.pageScrollTo({
						scrollTop: 999999, // 设置一个超大值，以保证滚动条滚动到底部
						duration: 0
					});
				})
			},
			// 表情
			showEmoji() {
				this.emojShow = !this.emojShow;
				if (this.otherTypesMessagePanelVisible) {
					this.otherTypesMessagePanelVisible = false;
				}
			},
			selemoji(m) {
				this.emojShow = false;
				this.content += m;
			},
			// 切换上传图片等组件
			showOtherTypesMessagePanel() {
				this.otherTypesMessagePanelVisible = !this.otherTypesMessagePanelVisible;
				if (this.emojShow) {
					this.emojShow = false;
				}
			}
		}
	}
</script>

<style lang="scss">
	.warp {
		min-height: 100vh;
		background-color: #F7F6FB;

		.header {}

		.scroll-view {
			padding: 20rpx 20rpx 140rpx 20rpx;
			text-align: center;

			.talk-list {

				/* 消息项，基础类 */
				.item {
					padding: 20rpx 0rpx 0 0rpx;
					align-items: flex-start;
					align-content: flex-start;
					color: #333;

					.pic {
						width: 74rpx;
						height: 74rpx;
						border-radius: 50%;
						border: #fff solid 1px;
					}

					.content {
						padding: 5rpx 20rpx;
						border-radius: 4px;
						max-width: 500rpx;
						word-break: break-all;
						line-height: 74rpx;
						position: relative;
					}

					/* 收到的消息 */
					&.pull {
						.content {
							margin-left: 32rpx;
							background-color: #fff;

							&::after {
								content: '';
								display: block;
								width: 0;
								height: 0;
								border-top: 16rpx solid transparent;
								border-bottom: 16rpx solid transparent;
								border-right: 20rpx solid #fff;
								position: absolute;
								top: 12rpx;
								left: -18rpx;
							}
						}

						.content2 {
							margin-left: 32rpx;

							image {
								width: 200rpx;
								height: 150rpx;
							}

							&::after {
								content: '';
								display: block;
								width: 0;
								height: 0;
								border-top: 16rpx solid transparent;
								border-bottom: 16rpx solid transparent;
								position: absolute;
								top: 12rpx;
								right: -18rpx;
							}
						}
					}

					/* 发出的消息 */
					&.push {
						/* 主轴为水平方向，起点在右端。使不修改DOM结构，也能改变元素排列顺序 */
						flex-direction: row-reverse;
						display: flex;

						.content {
							margin-right: 32rpx;
							background-color: #53B3D9;
							color: #fff;

							image {
								width: 200rpx;
								height: 150rpx;
							}

							&::after {
								content: '';
								display: block;
								width: 0;
								height: 0;
								border-top: 16rpx solid transparent;
								border-bottom: 16rpx solid transparent;
								border-left: 20rpx solid #53B3D9;
								position: absolute;
								top: 12rpx;
								right: -18rpx;
							}
						}

						.content2 {
							margin-right: 32rpx;

							image {
								width: 200rpx;
								height: 150rpx;
							}

							&::after {
								content: '';
								display: block;
								width: 0;
								height: 0;
								border-top: 16rpx solid transparent;
								border-bottom: 16rpx solid transparent;
								position: absolute;
								top: 12rpx;
								right: -18rpx;
							}
						}
					}
				}
			}
		}

		.action-box {
			display: flex;
			backdrop-filter: blur(0.27rpx);
			width: 100%;
			position: fixed;
			bottom: 0;
			left: 0;
			flex-direction: column;
			background-color: #FFFFFF;

			.slider {
				max-width: 750px;
				height: 256rpx;
				background-color: #fff;
				padding-bottom: 30rpx;

				&-emoji {
					width: 750rpx;
					// flex-direction: row;
					display: flex;
					align-items: center;
					flex-wrap: wrap;
					justify-content: center;

					&-icon {
						font-size: 40rpx;
						width: 5%;
						text-align: center;
						padding: 0rpx 30rpx 0 30rpx;
					}
				}

				/deep/.uni-swiper-slides {
					margin-bottom: 20rpx;
				}

				/deep/ .uni-swiper-dots-horizontal {
					bottom: 0;
				}
			}

			.action-top {
				display: flex;
				padding: 20rpx 10rpx;
				backdrop-filter: blur(0.27rem);
				background: #FAFAFA;
				width: 100%;
				align-items: center;

				.record-icon {
					font-size: 32rpx;
					text-align: center;
					// background: url("./static/images/record-appearance-icon.png") no-repeat center;
					background-size: 50%;

					image {
						width: 50rpx;
					}
				}

				.message-input {
					border-radius: 60rpx;
					background: #fff;
					height: 80rpx;
					margin-left: 10rpx;

					input {
						width: 400rpx;
						height: 80rpx;
						line-height: 80rpx;
						padding-left: 20rpx;
						font-size: 28rpx;
					}
				}

				.file-icon {
					background-size: 70%;
					color: #9D9D9D;
					position: relative;
					// line-height: 75rpx;
					padding: 0 10rpx;

					image {
						width: 50rpx;
					}
				}

				.send-message-btn {
					font-size: 30rpx;
					width: 80rpx;
					color: #fff;
					background: #53B3D9;
					border-radius: 30rpx;
					padding: 10rpx 20rpx;
					text-align: center;
				}
			}

			.action-bottom {
				.action-flex {
					display: flex;
					align-items: center;
					background-color: #F9F9F8;

					.more-item {
						text-align: center;
						padding: 20rpx;
						color: #666666;

						.more-img {
							background: #fff;
							padding: 40rpx;
							margin-bottom: 10rpx;

							image {
								width: 50rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
